package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.JsonObject;

import Utility.Base64;
import Utility.mail;

import DB.CouchDB;

public class Reset extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Reset() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);

	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + path + "/";

		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		JsonObject object = new JsonObject();

		String email;
		String token;
		HttpSession session = request.getSession(true);
		if (session.getAttribute("email") == null
				|| session.getAttribute("token") == null) {
			object.addProperty("message",
					"The session is expired ,please log in first... ");
		} else {
			email = session.getAttribute("email").toString();
			token = session.getAttribute("token").toString();
			CouchDB db = new CouchDB();

			if (!mail.noDupEmail(email, System.currentTimeMillis(), "",
					CouchDB.RETDB)) {

				object
						.addProperty("message",
								"You can only request a password-reseting Email for every 5 minutes");

			} else {
				String random = "";
				String newPwd;
				for (int i = 0; i < 5; i++)
					random = random + (int) (Math.random() * 100);
				newPwd = Base64.EncoderBase64(random);
				db.deleteUser(email);
				db.createUser(email, newPwd);

				mail.sendPasswdMail(email, newPwd);
				session.invalidate();
				session = request.getSession(true);
				session.setAttribute("email", email);
				session.setAttribute("token", token);

				object
						.addProperty(
								"message",
								"you have changed your pass word successfully,\n"
										+ "A Email of your new password has been sent to you, please check it,/n"
										+ "this page will be redirected to home page, please wait...");

			}

		}
		out.print(object);
		out.flush();
		out.close();

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
